package com.hxzy.system.service.impl;

import com.hxzy.common.core.domain.entity.DictType;
import com.hxzy.common.exception.CustomException;
import com.hxzy.system.dto.DictTypeDTO;
import com.hxzy.system.mapper.DictDataMapper;
import com.hxzy.system.mapper.DictTypeMapper;
import com.hxzy.system.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能描述
 *
 * @author tonneyyy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictTypeServiceImpl extends CommonServiceImpl<DictType,Long> implements DictTypeService {

    private DictTypeMapper  dictTypeMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    public void setDictTypeMapper(DictTypeMapper dictTypeMapper) {
        this.dictTypeMapper = dictTypeMapper;
        super.setMyBatisBaseDao(dictTypeMapper);
    }

    /**
     * 根据条件分页查询字典类型
     * @param dictTypeDTO
     * @return
     */
    @Override
    public List<DictType> select(DictTypeDTO dictTypeDTO) {
        return this.dictTypeMapper.select(dictTypeDTO);
    }

    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        for(Long dictId: dictIds){
            DictType dictType = this.dictTypeMapper.selectByPrimaryKey(dictId);
            int i = this.dictDataMapper.countDictDataByType(dictType.getDictType());
            if(i>0){
                throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
        }

        int count=this.dictTypeMapper.deleteDictTypeByIds(dictIds);
        return count;

    }
}


